package com.sx.pipeline.ui.needs.construction.approve

import android.app.Application
import com.blankj.utilcode.util.ToastUtils
import com.sx.mvvmlibs.base.BaseViewModel
import com.sx.mvvmlibs.event.SingleLiveEvent
import com.sx.mvvmlibs.http.CommonObserver
import com.sx.mvvmlibs.utils.RxThreadHelper
import com.sx.pipeline.R
import com.sx.pipeline.app.model.CYBaseViewModel
import com.sx.pipeline.repos.http.req.AuditTaskDTO
import com.sx.pipeline.repos.http.res.Ipage
import com.sx.pipeline.repos.http.res.ReleaseNeedsVO
import com.sx.pipeline.repos.http.res.ReleaseSupplyVo
import com.sx.pipeline.ui.task.entity.ReleaseTaskListVO

/**
 * @Author: YY
 * @Date: 2022/4/13
 * @Description: TODO
 */
class ApprovalNeedsVM(app: Application): CYBaseViewModel(app) {

    /**
     * 获取我发布的任务类型标题
     */
    fun getApprovalRequireTabsList(): List<String> {
        val tabs = mutableListOf<String>()
        tabs.add(getApplication<Application>().getString(R.string.txt_pending_review))
        tabs.add(getApplication<Application>().getString(R.string.txt_passed))
        tabs.add(getApplication<Application>().getString(R.string.txt_rejected))
        return tabs
    }


    /**
     * 获取审批需求列表
     */
    var page=1
    val approvalNeedsListEvent: SingleLiveEvent<Ipage<ReleaseNeedsVO>> = SingleLiveEvent()
    fun auditNeedPage(status: Int,page:Int=1) {
        addSubscribe(
            mModel.auditNeedPage(status,page)
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object : CommonObserver<Ipage<ReleaseNeedsVO>, BaseViewModel<*>>
                    (this, true) {
                    override fun onResult(result: Ipage<ReleaseNeedsVO>) {
                        approvalNeedsListEvent.postValue(result)
                        mUI.initRefreshEvent().postValue(true)
                        if (page != 1) {
                            mUI.initLoadEvent().postValue(true)
                        }
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        mUI.initRefreshEvent().postValue(false)
                        if (page != 1) {
                            mUI.initLoadEvent().postValue(true)
                        }
                    }
                })
        )
    }

    /**
     * 审核任务
     * @param id 任务id
     * @param remark 驳回原因
     * @param status 1审核通过  2审核驳回
     */
    val auditSuccessEvent: SingleLiveEvent<Boolean> = SingleLiveEvent()
    fun getNeedOrderAudit(id: Int,remark: String,status:Int) {
        val dto= AuditTaskDTO(id,remark,status)
        addSubscribe(
            mModel.getNeedOrderAudit(dto)
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object : CommonObserver<Any, BaseViewModel<*>>
                    (this, true) {
                    override fun onResult(result: Any) {
                        auditSuccessEvent.postValue(true)
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        ToastUtils.showShort(msg)
                    }
                })
        )
    }


    /**
     * 需求详情
     */
    val needOrderDetailEvent: SingleLiveEvent<ReleaseNeedsVO> = SingleLiveEvent()
    fun getNeedOrderDetail(id: Int) {
        addSubscribe(
            mModel.getNeedOrderDetail(id)
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object : CommonObserver<ReleaseNeedsVO, BaseViewModel<*>>
                    (this, true) {
                    override fun onResult(result: ReleaseNeedsVO) {
                        needOrderDetailEvent.postValue(result)
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        ToastUtils.showShort(msg)
                    }
                })
        )
    }
}